def fields(self,new_doc):
para = new_doc.add_paragraph("Table of Content")
para.alignment = WD_ALIGN_PARAGRAPH.CENTER
for run in para.runs:
run.font.name = 'Arial'
run.font.size = Pt(14)
run.bold = True
run.underline = True
paragraph = new_doc.add_paragraph()
paragraph.paragraph_format.space_before = Inches(0)
paragraph.paragraph_format.space_after = Inches(0)
run = paragraph.add_run()
fldChar = OxmlElement('w:fldChar') # creates a new element
fldChar.set(qn('w:fldCharType'), 'begin') # sets attribute on element
instrText = OxmlElement('w:instrText')
instrText.set(qn('xml:space'), 'preserve') # sets attribute on element
instrText.text = 'TOC \\o "1-3" \\h \\z \\u' # change 1-3 depending on heading levels you need
fldChar2 = OxmlElement('w:fldChar')
fldChar2.set(qn('w:fldCharType'), 'separate')
fldChar3 = OxmlElement('w:t')
fldChar3.text = "Right-click to update field."
fldChar3 = OxmlElement('w:updateFields')
fldChar3.set(qn('w:val'), 'true')
fldChar2.append(fldChar3)
fldChar4 = OxmlElement('w:fldChar')
fldChar4.set(qn('w:fldCharType'), 'end')
r_element = run._r
r_element.append(fldChar)
r_element.append(instrText)
r_element.append(fldChar2)
r_element.append(fldChar4)
p_element = paragraph._p
this is the code which generate automatically table of content.
but i have a doubt how can be create List of Figure page which contain all the figure captions and page number toggle field.